package com.tj.collection.controller.lending.response;


import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.EntityUtils;
import com.tj.collection.db.entity.ClMerchantEntity;
import com.tj.collection.db.entity.ClStageEntity;
import com.tj.collection.db.entity.business.ClOrderAllocationEntity;
import com.tj.collection.enums.CollectionStatusEnums;
import com.tj.collection.utils.CalculatorUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/9/10
 * Time: 下午5:15
 */
public class ClOrderQueryExpResponse implements ResponseData {

    private List<String[]> itemList;

    public ClOrderQueryExpResponse(List<ClOrderAllocationEntity> clOrderAllocationEntityList, List<ClStageEntity> clStageEntityList, List<ClMerchantEntity> clMerchantEntityList) {
        Map<String, ClMerchantEntity> clMerchantEntityMap = EntityUtils.makeEntityMap(clMerchantEntityList, "merchantCode");
        Map<String, ClStageEntity> clStageEntityMap = EntityUtils.makeEntityMap(clStageEntityList, "stageCode");
        this.itemList = new ArrayList<>();
        String[] row1 = {
                "Merchant", "Product", "Loan Id", "Customer Name", "Mobile Number", "Loan Amount",
                "Due Amount", "Total Amount", "DPD days", "Bucket", "Agent Name",
                "Language", "Collection Status", "Active tag"
        };

        this.itemList.add(row1);
        if (clOrderAllocationEntityList.size() > 0) {
            for (int i = 0; i < clOrderAllocationEntityList.size(); i++) {
                ClOrderAllocationEntity clOrderAllocationEntity = clOrderAllocationEntityList.get(i);

                BigDecimal expireAmount = new BigDecimal(0);//应还总金额
                BigDecimal lateFee = new BigDecimal(0);//总罚息
                if (clOrderAllocationEntity.getOverdueDays() > 0) {
                    BigDecimal overdueFee = CalculatorUtils.calculateOverdueFee(clOrderAllocationEntity.getBorrowCapital(),
                            clOrderAllocationEntity.getLateFeeRatio(),
                            clOrderAllocationEntity.getOverdueDays());
                    lateFee = overdueFee.add(clOrderAllocationEntity.getLateServiceFee());
                    expireAmount = CalculatorUtils.repaymentTotalCapital(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getInterest(),
                            overdueFee, clOrderAllocationEntity.getLateServiceFee());
                } else {
                    lateFee = new BigDecimal(0);
                    expireAmount = CalculatorUtils.repaymentTotalCapital(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getInterest(),
                            BigDecimal.ZERO, BigDecimal.ZERO);
                }
                expireAmount = expireAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
                lateFee = lateFee.setScale(2, BigDecimal.ROUND_HALF_UP);

                ClStageEntity clStageEntity = clStageEntityMap.get(clOrderAllocationEntity.getStageCode());
                String[] row = new String[row1.length];

                row[0] = clOrderAllocationEntity.getSourceMerchantCode();
                row[1] = clMerchantEntityMap.containsKey(clOrderAllocationEntity.getSourceMerchantCode()) ? clMerchantEntityMap.get(clOrderAllocationEntity.getSourceMerchantCode()).getAppName() : clOrderAllocationEntity.getSourceMerchantCode();
                row[2] = clOrderAllocationEntity.getOrderNo();
                row[3] = clOrderAllocationEntity.getName();
                row[4] = clOrderAllocationEntity.getMobile();
                row[5] = clOrderAllocationEntity.getBorrowCapital().setScale(2).toString();
                row[6] = lateFee.setScale(2).toString();
                row[7] = expireAmount.setScale(2).toString();
                row[8] = clOrderAllocationEntity.getOverdueDays() + "";
                row[9] = clStageEntity != null ? clStageEntity.getStageName() : clOrderAllocationEntity.getStageCode();
                row[10] = clOrderAllocationEntity.getAgentName();
                row[11] = clOrderAllocationEntity.getUserLanguage();
                try {
                    row[12] = CollectionStatusEnums.getEnum(Integer.valueOf(clOrderAllocationEntity.getCommentType())).getShortDesc();
                } catch (Exception e) {
                    row[12] = "";
                }
                row[13] = clOrderAllocationEntity.getLastActiveDate() != null
                        && DateUtil.getDate("1970-01-01", "yyyy-MM-dd").getTime() != clOrderAllocationEntity.getLastActiveDate().getTime()
                        ? "TRUE" : "FALSE";
                this.itemList.add(row);
            }
        }
    }
}
